package cn.yunhe.servlet;

import cn.yunhe.dao.IMerchantsAccount;

import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 王羊
 */
@WebServlet("shangjia/MerchantsAccountServletZC")
public class MerchantsAccountServletZC extends HttpServlet {
    SqlSession sqlSession = null;
    IMerchantsAccount iMerchantsAccount = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //获取输入的注册名和密码
        String a = request.getParameter("account");
        String b = request.getParameter("password");
        String c = request.getParameter("repassword");
        //判断两次输入密码是否相同
        if (b.equals(c)) {
            sqlSession = SqlSessionFactoryUtil.getSqlSession();
            iMerchantsAccount = sqlSession.getMapper(IMerchantsAccount.class);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date());
            Map map = new HashMap();
            map.put("account", a);
            int in = iMerchantsAccount.getAllMerchantsAccount(map);
            //判断查询到的个数是否为零
            if (in == 0) {
                map.put("password", b);
                map.put("flag", 0);
                map.put("createdate", date);

                boolean bl = iMerchantsAccount.zhuce(map);
                sqlSession.commit();
                //判断插入数据是否成功
                if (bl) {

                    out.print("<script>alert('注册成功');location.href='register.html';</script>");
                }
            } else {

                //该用户已存在 跳转至注册页面

                out.print("<script>alert('该用户已存在');location.href='register.html';</script>");

            }
        } else {
            out = response.getWriter();
            //两次密码输入不一致 跳转至注册页面

            out.print("<script>alert('两次密码输入不一致');location.href='register.html'</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

